package com.cskaoyan.service;

import com.cskaoyan.bean.data.CommentListData;
import com.cskaoyan.bean.data.CountCommentData;
import com.cskaoyan.bean.data.UserInfoData;
import com.cskaoyan.domain.MarketComment;
import com.cskaoyan.domain.MarketCommentExample;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.mapper.MarketCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MarketCommentServiceImpl implements MarketCommentService{
    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketUserService userService;

    @Override
    public List<MarketComment> selectCommentByValueId(Integer id) {
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentCriteria = commentExample.createCriteria();
        commentCriteria.andDeletedEqualTo(false).andValueIdEqualTo(id);
        commentExample.setOrderByClause("'add_time' DESC");
        List<MarketComment> marketComments = commentMapper.selectByExample(commentExample);
        return marketComments;
    }

    @Override
    public List<CommentListData> queryCommentsByValueIdAndType(Integer valueId, Byte type, Boolean showType) {
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentCriteria = commentExample.createCriteria();
        commentCriteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);

        //showByte: false 显示所有类型的图片
        if(showType){
            commentCriteria.andHasPictureEqualTo(showType);
        }
        List<MarketComment> marketComments=commentMapper.selectByExample(commentExample);
        List<CommentListData> dataList=new ArrayList<>();
        for (MarketComment marketComment : marketComments) {
            CommentListData data = new CommentListData();
            LocalDateTime localDateTime = marketComment.getAddTime();
            Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
            data.setAddTime(date);
            data.setAdminContent(marketComment.getAdminContent());
            data.setContent(marketComment.getContent());
            data.setPicList(marketComment.getPicUrls());

            UserInfoData userInfo = new UserInfoData();
            Integer userId = marketComment.getUserId();
            MarketUser marketUser = userService.queryByUserId(userId);
            userInfo.setAvatarUrl(marketUser.getAvatar());
            userInfo.setNickName(marketUser.getNickname());
            data.setUserInfo(userInfo);
            dataList.add(data);

        }
        return dataList;
    }

    @Override
    public Long countComments(Integer id) {
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false).andValueIdEqualTo(id);
        long count = commentMapper.countByExample(commentExample);
        return count;
    }

    @Override
    public CountCommentData countImageOrNoImage(Integer valueId, Byte type) {
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        List<MarketComment> commentsList = commentMapper.selectByExample(commentExample);
        long allCount = commentsList.stream().count();
        long hasPicCount = commentsList.stream().filter(marketComment -> marketComment.getHasPicture() == true).count();

        CountCommentData commentData = new CountCommentData();
        commentData.setAllCount((int)allCount);
        commentData.setHasPicCount((int)hasPicCount);

        return commentData;
    }

    @Override
    public int updateTopicComment(Map map, Integer userId) {
        MarketComment marketComment = new MarketComment();
        marketComment.setContent(map.get("content").toString());
        marketComment.setHasPicture(Boolean.valueOf(map.get("hasPicture").toString()));
        String[] picUtls={map.get("picUrls").toString()};
        marketComment.setPicUrls(picUtls);
        marketComment.setStar((Short.valueOf(map.get("star").toString())));
        marketComment.setType( Byte.valueOf(map.get("type").toString()));
        marketComment.setValueId( Integer.valueOf(map.get("valueId").toString()));
        Date now = new Date();
        marketComment.setAddTime(LocalDateTime.now());
        marketComment.setUpdateTime(LocalDateTime.now());
        marketComment.setUserId(userId);

        return commentMapper.insertSelective(marketComment);
    }
}
